package com.tenement.controller;

import com.tenement.common.Axios;
import com.tenement.entity.Menu;
import com.tenement.enums.RespBusiness;
import com.tenement.service.MenuService;
import com.tenement.vo.QueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 *  控制层。
 *
 * @author BertXie
 * @since 1.0.1
 */
@RestController
@RequestMapping("/menu")
@RequiredArgsConstructor
@Api(tags = "菜单")
@Validated
public class MenuController {

    private final MenuService menuService;

    @ApiOperation(value = "操作数据")
    @PreAuthorize("@exp.hasAuthority('system:menu:operator')")
    @PutMapping("operator")
    public Axios operator(@RequestBody @Validated Menu menu) {
        return menuService.operator(menu) >= 1? Axios.success(RespBusiness.SUCCESS) : Axios.error(RespBusiness.FAIL);
    }

    @ApiOperation(value = "获取菜单数据")
    @PreAuthorize("@exp.hasAuthority('system:menu:list')")
    @PostMapping("list")
    public Axios list(@RequestBody QueryVo queryVo) {
        return Axios.success(menuService.list(queryVo));
    }

    @ApiOperation(value = "删除菜单")
    @PreAuthorize("@exp.hasAuthority('system:menu:remove')")
    @DeleteMapping("delete/{id}")
    public Axios delete(@PathVariable Long id) {
        return menuService.delete(id) >= 1 ? Axios.success("菜单删除成功") : Axios.error(RespBusiness.FAIL);
    }

    @ApiOperation(value = "根据id获取菜单")
    @GetMapping("id/{id}")
    public Axios id(@PathVariable Long id) {
        return Axios.success(menuService.getById(id));
    }

    @ApiOperation(value = "获取菜单的树结构")
    @GetMapping(value = { "treeNode" , "treeNode/{keyWord}" })
    public Axios treeNode(@PathVariable(required = false)String keyWord) {
        return Axios.success(menuService.treeNode(keyWord));
    }

    @ApiOperation(value = "获取角色拥有的菜单")
    @GetMapping("roleMenu/{roleId}")
    public Axios roleMenu(@PathVariable Long roleId) {
        return Axios.success(menuService.roleMenu(roleId));
    }

}
